package business.impl;

import java.sql.ResultSet;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

import model.TPractice;
import model.TWanted;
import model.VWanted;
import business.basic.iHibBaseDAO;
import business.basic.iHibBaseDAOImpl;
import business.dao.WantedDao;

public class WantedDaoImpl implements WantedDao {
	
	
	iHibBaseDAO bdao = null;
	public void setBdao (iHibBaseDAO bdao){
		this.bdao=bdao;
		
	}
	

	@Override
	public TWanted getWantedById(int id) {
		TWanted title = (TWanted) bdao.findById(TWanted.class,id);
		return title;
	}

	@Override
	public int addWanted(TWanted model) {
		Object obj = bdao.insert(model);
		if (obj == null) {
			return 0;
		}
		return 1;
	}

	@Override
	public boolean upWanted(TWanted model) {
		return bdao.update(model);
	}
	
	

	@Override
	public TWanted getWanteds(int id) {
		String sql = " from TWanted where stuinfoid =   ?";
		Object[] para = {id};
		TWanted modl = (TWanted) bdao.selectby(sql,para);
		return modl;
	}


	@Override
	public boolean delWanted(int id) {
		return bdao.delete(TWanted.class, id);
	}


	@Override
	public VWanted getV_Wanteds(int stuid) {
		String sql = " from VWanted where stuinfoid =   ?";
		Object[] para = {stuid};
		VWanted modl =   (VWanted) bdao.selectby(sql,para);
		return modl;
	}


	@Override
	public List<VWanted> getV_Wantedsbypost(String search,String[] post,String[] workaddress
			,String[] shcool ,int huntingstatus,int stulevel) {
		String sql = "  from VWanted";
				sql+= "  where isDel = 0 ";
				if(post.length != 0){
				sql+= " and  (post like '%"+post[0]+"%'";
				for (int i = 0; i < post.length; i++) {
					sql+= " or post like '%"+post[i]+"%'";
				}
				sql+= " )";
				}
				if(workaddress.length != 0){
				sql+= " and (workaddress like '%"+workaddress[0]+"%'";
				for (int i = 0; i < workaddress.length; i++) {
					sql+= " or workaddress like '%"+workaddress[i]+"%'";
				}
				sql+= ")";
				}
				if(shcool.length != 0){
				sql+= " and (shcool like '%"+shcool[0]+"%'";
				for (int i = 0; i < shcool.length; i++) {
					sql+= " or shcool like '%"+shcool[i]+"%'";
				}
				sql+= ")" ;
				}
				if(huntingstatus != 0){
						sql += " and huntingstatus  = "+huntingstatus;
				}if(stulevel != 0) {
					
					sql += "  and stulevel = "+stulevel;
				
			}
				sql+= " and post like '%"+search+"%' or workaddress like '%"+search+"%'";
				sql+= " or stuname like '%"+search+"%'";
				sql+= " or shcool like '%"+search+"%'" ;
				sql+= " or majorname like '%"+search+"%'" ;
				sql+= " or classname like '%"+search+"%'" ;
				sql+= " or college like '%"+search+"%'" ;
				
				sql	+= "order by  completiondegree desc ";

				List<VWanted> modl = bdao.select(sql);
				return modl;	
	}


	@Override
	public List<VWanted> getV_Wantedsby(String search, String post,
			String workaddress, String shcool, int huntingstatus,
			int stulevel) {
		String sql = "  from VWanted";
		sql+= "  where isDel = 0 ";
		if(post != ""){
		sql+= " and  post like '%"+post+"%'";
		

		}
		if(workaddress != ""){
		sql+= " and workaddress like '%"+workaddress+"%'";
		

		}
		if(shcool != ""){
		sql+= " and school like '%"+shcool+"%'";
		

		}
		if(huntingstatus != 0){
				sql += " and huntingstatus  = "+huntingstatus;
		}if(stulevel != 0) {
			
			sql += "  and stulevel = "+stulevel;
		
	}
		if (search != null) {
			
		
		sql+= " and post like '%"+search+"%' or workaddress like '%"+search+"%'";
		sql+= " or stuname like '%"+search+"%'";
		sql+= " or school like '%"+search+"%'" ;
		sql+= " or majorname like '%"+search+"%'" ;
		sql+= " or classname like '%"+search+"%'" ;
		sql+= " or college like '%"+search+"%'" ;
		}
		sql	+= "order by  completiondegree desc ";

		List<VWanted> modl = bdao.select(sql);
		return modl;	
	}
	
		

}
